﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
using ExitGames.Client.Photon;

namespace TestClient
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private PhotonPeer Peer { get; set; }
        public bool PeerConnecting { get; set; }
        public bool PeerConnected { get; set; }
        public DispatcherTimer DispatcherTimer { get; set; }

        public MainWindow()
        {
            InitializeComponent();
            PeerConnecting = false;
            PeerConnected = false;
            PhotonListener listener = new PhotonListener {Main = this};
            Peer = new PhotonPeer(listener, ConnectionProtocol.Udp);
            Disconnect_Button.IsEnabled = false;
            Login_Button.IsEnabled = false;
            DispatcherTimer = new DispatcherTimer();
            DispatcherTimer.Tick += dispatcherTimer_tick;
            DispatcherTimer.Interval = new TimeSpan(0,0,0,1);
            StatusIndicator.Fill = new SolidColorBrush(Colors.Red);
            StatusTextBox.Foreground = new SolidColorBrush(Colors.Black);
        }
        private void Connect_Button_OnClick(object sender, RoutedEventArgs e)
        {
            StatusTextBox.AppendText(DateTime.Now.ToString("yy-mm-dd hh.mm.ss.ffffff") + " - Trying to connect to " + IPAddress.Text + " on default port 5055...\n");
            Connect_Button.IsEnabled = false;
            if (Peer.Connect(IPAddress.Text + ":5055", "MasterServer"))
            {
                PeerConnecting = true;
                StatusTextBox.AppendText(DateTime.Now.ToString("yy-mm-dd hh.mm.ss.ffffff") + " - Connection initialization in progress...\n");
                DispatcherTimer.Start();
            }
            else
            {
                StatusTextBox.AppendText(DateTime.Now.ToString("yy-mm-dd hh.mm.ss.ffffff") + " - Connection initialization failed...\n");
            }
        }
        private void Disconnect_Button_OnClick(object sender, RoutedEventArgs e)
        {
            StatusTextBox.AppendText(DateTime.Now.ToString("yy-mm-dd hh.mm.ss.ffffff") + " - Trying to disconnect from " + IPAddress.Text + "...\n");
            Peer.Disconnect();
        }

        private void dispatcherTimer_tick(object sender, EventArgs e)
        {
            Service();
            if (PeerConnected)
            {
                StatusIndicator.Fill = new SolidColorBrush(Colors.Green);
            } else if (PeerConnecting && !PeerConnected)
            {
                StatusIndicator.Fill = new SolidColorBrush(Colors.Orange);
            }
            else
            {
                StatusIndicator.Fill = new SolidColorBrush(Colors.Red);
            }
            CommandManager.InvalidateRequerySuggested();
        }
        private void Service()
        {
            Peer.Service();
        }

        private void Login_Button_OnClick(object sender, RoutedEventArgs e)
        {
            if (Username.Text != "" && Password.Password != "")
            {
                OperationRequest operation = new OperationRequest();
                Dictionary<byte, object> parameters = new Dictionary<byte, object>
                                                      {
                                                          {1, Username.Text},
                                                          {2, Password.Password}
                                                      };
                operation.OperationCode = 0x01;
                operation.Parameters = parameters;

                Peer.OpCustom(operation, true, 0, false);
            }
        }
    }
}
